package at.im.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.corba.se.impl.util.Version;

public class SQLConnection 
{
	public SQLConnection()
	{}
	
	public ArrayList<Object> connectToDatabase(int request, Object object, int type, String searchstatement)
	{
		Connection con	= null;
        Statement st 	= null;
        ResultSet rs 	= null;
        
        ArrayList<Object> list = new ArrayList<Object>();

        String url 			= "jdbc:mysql://localhost:3306/testdb";
        String user 		= "testuser";
        String password 	= "test623";

        try {
            con = DriverManager.getConnection(url, user, password); // connect
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) 
            {
                System.out.println(rs.getString(1));
            }
            
            switch(request)
            {
            	case 0: SQLWrite.write(con, object, type);							break;
            	case 1: list = SQLRead.read(con, type, rs, searchstatement);		break;
            }
            
            rs.close();
            st.close();
            con.close();
        } catch (Exception ex) 
        {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
        
        return list;
	}
}
